﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ExcelSysLibs;

namespace FileSC.Configs
{
    public class ExcelConfig
    {
        public string Name { get; internal set; }
        public string ConfigPath { get; internal set; }
        public string SheetName { get; internal set; }
        public string Key { get; internal set; }
        public string FilePath { 
            get
            {
                return Path.Combine(ConfigPath, Name + ".config.xlsx");
            }
        }
        public ExcelConfig(string name)
        {
            Name = name;
            this.ConfigPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        }

        public static ExcelConfig Load(string path, string name)
        {
            var cfg = new ExcelConfig(name);
            cfg.SetPath(path);
            return cfg;
        }
        public void SetPath(string path)
        {
            this.ConfigPath = path;
        }
        public void WriteConfig(string sheetname, string key, string header, string value)
        {
            this.SheetName = sheetname;
            this.Key = key;
            if (!File.Exists(FilePath)) goto ERROR;
            var sheets = ExcelSys.GetSheetNames(FilePath);
            if (!sheets.Contains(sheetname)) goto ERROR;

            var sheet = ExcelSys.Query(FilePath, true, sheetname).ToArray();
            for(var i = 0; i< sheet.Count(); i++)
            {
                var dic = sheet[i] as IDictionary<string, object>;
                if (dic["名称"].Equals(key))
                {
                    if (dic[header].Equals(value)) return;
                    dic[header] = value;
                    sheet[i] = dic;
                    ExcelSys.SaveAs(FilePath, sheet, true, sheetname, overwriteFile: true);
                    return;
                }
            }

        ERROR:
            return;
        }
        public T ReadConfig<T>(string sheetname, string key, string header)
        {
            this.SheetName = sheetname;
            this.Key = key;
            if (!File.Exists(FilePath)) goto ERROR;
            var sheets = ExcelSys.GetSheetNames(FilePath);
            if (!sheets.Contains(sheetname)) goto ERROR;

            var sheet = ExcelSys.Query(FilePath, true, sheetname);
            var item = sheet.Cast<IDictionary<string, object>>().Where(t => t["名称"].Equals(key)).FirstOrDefault();

            if (item == null) goto ERROR;
            return (T)item[header];


        ERROR:
            return default;
        }
        public void WriteValue(string sheetname, string key, string value)
        {
            this.SheetName = sheetname;
            this.Key = key;
            WriteConfig(sheetname, key, "值", value);
        }
        public T ReadValue<T>(string sheetname, string key)
        {
            this.SheetName = sheetname;
            this.Key = key;
            return ReadConfig<T>(sheetname, key, "值");
        }
        public void WriteGlobal(string key, string value)
        {
            WriteValue("全局", key, value);
        }
        public T ReadGlobal<T>(string key)
        {
            return ReadValue<T>("全局", key);
        }
        public T ReadOther<T>(string name)
        {
            return ReadConfig<T>(SheetName, Key, name);
        }
        public T ReadOtherKeyValue<T>(string name)
        {
            return ReadValue<T>(SheetName, name);
        }

        public void Save()
        {
            var sheets = ExcelSys.GetSheetNames(FilePath);
            ExcelSys.SaveAs(FilePath, sheets);
        }
    }
}
